import request from '@/assets/utils/request'
import type {CartItem} from '@/types/cart'
import type {Order} from '@/types/order'

// 合并本地购物车到线上
export const mergeLocalCart = (cartList: CartItem[]): Promise<any> =>
  request.post('member/cart/merge',
    cartList.map(i => (
      {skuId: i.skuId, count: i.count, selected: i.selected}
    ))
  )

// 获取线上购物车列表
export const getOnlineCart = (): Promise<any> => request.get('member/cart')

// 添加商品到线上购物车
export const addGoodToOnlineCart = (skuId: string, count: number): Promise<any> =>
  request.post('member/cart', {skuId, count})

// 从线上购物车中删除商品(传入skuId列表，可批量删除)
export const delGoodsInOnlineCart = (skuIds: string[]): Promise<any> =>
  request.delete('member/cart', {ids: skuIds})

// 更新线上购物车商品
export const updateGoodsInOnlineCart = (skuId: string, good: CartItem): Promise<any> =>
  request.put(`member/cart/${skuId}`, good)

// 提交订单
export const submitOrder = (order: Order): Promise<any> =>
  request.post('member/order', order)